﻿using HXDNB.DAL;
using HXDNB.Infrastructure;
using HXDNB.Infrastructure.Helper;
using HXDNB.Model;
using System;
using System.Linq;
using System.Web.Mvc;

namespace HXDNB.CMS.Controllers
{
    public class ArticleController : BaseController
    {
        /// <summary>
        /// 文章列表
        /// </summary>
        public ActionResult List(FormCollection form, int? MeunID, int index = 0)
        {
            var bll = new ArticleBLL();
            var list = bll.GetList(form["title"], MeunID);
            var pag = new Paging<ArticleListModel>(index, list);
            ViewBag.PagList = pag;
            return View(form);
        }

        /// <summary>
        /// 文章管理
        /// </summary>
        public ActionResult Info(int? id)
        {
            var info = new Article();
            info.Sort = int.Parse(DateTime.Now.ToString("yyyyMMdd"));
            if (id != null)
            {
                var bll = new ArticleBLL();
                info = bll.Get((int)id);
                if (info.MeunId > 0)
                {
                    System.Collections.Generic.List<Meun> meunlist = new System.Collections.Generic.List<Meun>();
                    new MeunBLL().GetParentList((int)info.MeunId, ref meunlist);
                    if (HttpContext.Session["MeunRole"].ToString().Contains("," + meunlist[0].ID + ",") == false)
                    {
                        throw new Exception("抱歉，您没有访问此文章的权限");
                    }
                }
            }
            return View(info);
        }

        /// <summary>
        /// 文章管理
        /// </summary>
        [HttpPost, ValidateInput(false)]
        public ActionResult Info(Article info)
        {
            var bll = new ArticleBLL();
            ViewBag.Result = true;
            if (string.IsNullOrWhiteSpace(Request.Form["Recommend"]) == false)//
            {
                info.Recommend = string.Format(",{0},", Request.Form["Recommend"]);
            }
            if (Request.Files["Thumbnail"].ContentLength != 0)//上传缩略图
            {
                string filename = Upload.UpLoadImg("Thumbnail");
                info.Thumbnail = filename != null ? "img/" + filename : null;
            }
            info.Entity = string.IsNullOrEmpty(info.Entity) ? "" : info.Entity;
            info.Functions = string.IsNullOrEmpty(info.Functions) ? "" : info.Functions;
            info.Department = string.IsNullOrEmpty(info.Department) ? "" : info.Department;
            info.Location = string.IsNullOrEmpty(info.Location) ? "" : info.Location;
            info.Title = TextHelper.ReplaceCode(info.Title);
            info.Contents = TextHelper.ReplaceCode(info.Contents);
            info.MobileContents = TextHelper.ReplaceCode(info.MobileContents);
            if (info.Sort == null)
            {
                info.Sort = 0;
            }
            if (info.ID > 0)
            {
                ViewBag.Result = bll.Edit(info);
            }
            else
            {
                ViewBag.Result = bll.Add(info) > 0;
            }
            return View(info);
        }

        /// <summary>
        /// 文章删除
        /// </summary>
        [HttpPost]
        public string DelArticle(string id = "")
        {
            var bll = new ArticleBLL();
            bool result = true;
            foreach (string item in id.Split(',').Where(item => !string.IsNullOrWhiteSpace(item)))
            {
                result = bll.Delete(int.Parse(item));
                if (result == false)
                    break;
            }
            return result.ToString();
        }
    }
}
